package com.yayan.beautyclub.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yayan.beautyclub.bean.Category;
import com.yayan.beautyclub.service.ICategoryService;
import com.yayan.util.UploadUtil;

@Controller
@RequestMapping(value="/category", method=RequestMethod.POST)
public class CategoryController {

	@Autowired
	private ICategoryService categoryService;
	
	/**
	 * 后台管理，创建类别信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/create.do",produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String createCategory(MultipartFile pic,@RequestParam Map<String,String> map, HttpServletRequest request){
		//System.out.println(JSON.toJSON(map));
		Category category = new Category();
		if(pic != null){
			if(pic.getSize() >= 5242880 || !UploadUtil.checkExtention(pic))
				return "图片必须小于5M，且格式必须为jpg/jpeg/png";
			// 获取图片原始文件名
			String originalFilename = pic.getOriginalFilename();
			// 文件名使用当前时间
			String currentName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());				
			String url = request.getSession().getServletContext().getRealPath("") + "upload/";
			//System.out.println(url);
			File dir = new File(url);
			if(!dir.exists()) {
				dir.mkdirs();
			}
			currentName += "_category_." + FilenameUtils.getExtension(originalFilename);
			try{
				pic.transferTo(new File(url+currentName));
				category.setImage(currentName);
			}catch(Exception e){
				return "图片保存失败！请重试！";
			}
		}else{			
			return "提交失败！请选择图片！";
		}		
		
		if(map.get("categoryType").equals("0"))
			category.setParentId(0);
		else
			category.setParentId(Integer.parseInt(map.get("parentId")));		
		category.setcName(map.get("cName"));
		category.setIntroduce(map.get("introduce"));		
		if(categoryService.saveCategory(category) == 0)
			return "提交失败！请检查输入内容是否有误！";			
		return "true";
	}
	
	/**
	 * 后台管理/前台首页，获取类别列表
	 * @return
	 */
	@RequestMapping(value="/category_list.do")
	@ResponseBody
	public List<Category> getCategoryList(){
		return categoryService.getCategoryList();
	}
	
	/**
	 * 后台管理，根据parentId获取类别列表
	 * @param parent
	 * @return
	 */
	@RequestMapping(value="/get_category.do")
	@ResponseBody
	public List<Category> getCategory(@RequestParam String parent){		
		return categoryService.getCategoryList(Integer.parseInt(parent));
	}
	
	/**
	 * 后台管理，根据cId删除类别信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/delete_category.do")
	@ResponseBody
	public boolean deleteCategory(@RequestBody Map<String, String> map){
		int cId = Integer.parseInt(map.get("cId"));
		return categoryService.deleteCategory(cId);
	}
}
